<template>
    <div class="warpper-box">
        <form-create 
        v-model="params.fApi" 
        :rule="params.rule" 
        :option="params.option" 
        @btn-search-click="loadTable(true)"
        @btn-reset-click="reset"
        @btn-add-click="add(true)"/>
        <a-table :columns="table.columns" :pagination="table.pagination" @change="handleTableChange" rowKey="id" :data-source="table.loadData">
        </a-table>
         <a-modal title="新增" :width="640" :visible="form.visible" @ok="handleSubmit" @cancel="form.visible=false">
            <form-create v-model="form.fApi" :rule="form.rule" :option="form.option"/>
        </a-modal>
    </div>
</template>

<script>
import { 
    getSysTaskConfigList,
    getSysTaskConfigAdd,
    getSysTaskConfigEdit,
    setSysTaskConfigEnable
} from '@/api/manage/taskConfig'
import {
    PARAMS_TASK_CONFIG,
    FORM_TASK_CONFIG,
    COLUMNS_TASK_CONFIG,
    OPTION_BLOCK,
    OPTION_INLINE
} from './modules/form'
export default {
  name: 'taskConfig',
  data () {
    return {
        params:{
          fApi:{},
          rule:PARAMS_TASK_CONFIG,
          option:OPTION_INLINE
        },
        form:{
          id:'',
           visible:false,
          fApi:{},
          rule:[],
          option:OPTION_BLOCK
        },
        table:{
          columns:COLUMNS_TASK_CONFIG,
          loadData:[],
          pagination:{
            pageNum:1,
            total:0,
            pageSize: 11,
            showSizeChanger: true,
					showQuickJumper: true,
            pageSizeOptions: ["10", "20", "50", "100"],
            showTotal: total => `共有 ${total} 条数据`,
          }
        },
        fileUrl:''
    }
  },
  created () {
    this.loadTable(true);
  },
  methods: {
    async loadTable(flag){
      let params = {};
      if(JSON.stringify(this.params.fApi) !== "{}"){
        params.fieldName = this.params.fApi.getValue('fieldName');
      }
      if(flag){
        this.table.pagination.pageNum = 1;
        this.table.pagination.pageSize = 11;
      }
      params.pageNum = this.table.pagination.pageNum;
      params.pageSize = this.table.pagination.pageSize;
      const {code,data} = await getSysTaskConfigList(params);
      if(code ==='1'){
        this.table.loadData = data.content;
        this.table.pagination.total = data.total || 0;
      }
    },
    handleTableChange(pagination){
      this.table.pagination.pageNum = pagination.current;
      this.table.pagination.pageSize = pagination.pageSize;
      this.loadTable(false);
    },
    add(rows){
        this.form.rule = FORM_TASK_CONFIG;
        this.$nextTick(()=>{
            this.form.fApi.resetFields();
        });
        this.form.id = rows.id;
        this.form.visible = true;
    },
    handleSubmit(){
      this.form.fApi.submit(async (formData, $f) => {
        try {
          formData.id = this.form.id;
          const {code} = await getSysTaskConfigEdit(formData);
          if(code === '1'){
            this.form.visible = false;
            this.loadTable(true);
          }
        } catch (error) {
          this.$message.info(JSON.stringify(error));
        }
      })
    },
    async handelTempStatusChange(el){
      const status = el.status === 1 ? 2:1;
      let params = {
        status:status,
        id:el.id
      }
      const {code} = await setSysTaskConfigEnable(params);
      if(code === '1'){
        this.$message.success('操作成功');
        this.loadTable(false);
      }
    },
    reset(){
      this.params.fApi.resetFields();
      this.loadTable(true);
    }
  }
}
</script>




